package Week1.J0416;

import java.util.Scanner;

public class MinLenTwoStr {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String str1 = scanner.next();
        String str2 = scanner.next();
        String[] strArray = new String[n];

        if (str1 == null || str2 == null || n == 0) {
            System.out.println("-1");
            return;
        }

        int index1 = -1;
        int index2 = -1;
        int minLen = Integer.MAX_VALUE;

        for (int i = 0; i < strArray.length; i++) {
            strArray[i] = scanner.next();
        }

        for (int i = 0; i < strArray.length; i++) {
            if (str1.equals(strArray[i])) {
                index1 = i;
                if (index2 != -1) {
                    minLen = Math.min(minLen,index1-index2);
                }
            }
            if (str2.equals(strArray[i])) {
                index2 = i;
                if (index1 != -1) {
                    minLen = Math.min(minLen,index2-index1);
                }
            }
        }

        if (index1 == -1 || index2 == -1) {
            System.out.println("-1");
            return;
        }

        System.out.println(minLen);
    }
}
